<%@ page language="java" import="java.util.*" language="java" import="com.jspsmart.upload.*,java.sql.*"  pageEncoding="UTF-8"%>
<%@ page language="java" import="java.util.*" %>
<%@ page language="java" import="java.io.*" %>
<%@ page language="java" import="java.awt.*" %>
<%@ page language="java" import="java.awt.image.*" %>
<%@ page language="java" import="com.sun.image.codec.jpeg.*" %>
<%@ page import="RoverCommodity.RoverCommodityDeal"%>

<html>
  <head>
   
    <title>保存上传的信息</title>

  </head>
  
  <body>
 <%
 	
	   //不超过2M
	   int fileSizeAllow=2097152;
	   //可以用中文 
	   request.setCharacterEncoding("gbk");
	   // 新建一个SmartUpload对象
	    SmartUpload su = new SmartUpload();
	    RoverCommodityDeal ReoverCommodity=new RoverCommodityDeal();

	    // 上传初始化
	    su.initialize(pageContext);
	    // 上传文件
	    su.upload();
	    //取得指定位移处的文件对象file
	    com.jspsmart.upload.File file = su.getFiles().getFile(0);
	    
	    if (file.isMissing()){//文件是否存在
   %>
    <script language=javascript>
    alert("请先选择要上传的图片");
    window.location='RoverCommodity.jsp';
    </script>
    
   <%}
        //取得上载的文件的文件名
   		String fileName=file.getFileName();
   		//取得文件的大小
        int fileSize = file.getSize();
        String extName = file.getFileExt().toLowerCase();
        //取得后缀名
        // 将上传文件全部保存到指定目录
        if (fileSize > fileSizeAllow) {			
	   %>
	    <script language=javascript>
	    alert("文件太大了 ");
	    window.location='RoverCommodity.jsp';
	    </script>
	    
	   <%}
        if (!(extName.equals("gif")||extName.equals("jpg") )) {
	   %>
	    <script language=javascript>
	    alert("图片格式不正确  ");
	    window.location='RoverCommodity.jsp';
	    </script>
	    
	   <%}
	   String nowTime=new java.util.Date().toLocaleString().replaceAll(":","-").replaceAll(" ","-");
	   String fileNameNew=nowTime+"."+ extName;// 以日期命名的格式
	   file.saveAs("/UpLoadFile/"+fileNameNew, SmartUpload.SAVE_VIRTUAL);
	   String saveur="UpLoadFile"+"/"+fileNameNew;
	    String saveurl=request.getRealPath("/")+saveur;
	    //从跳转前页面获取参数 
	    
	    
		ReoverCommodity.setComID(ReoverCommodity.CalRCID());
		String RovID=(String)su.getRequest().getParameter("RovID");
        ReoverCommodity.setRovID(RovID);
		String ComID1=(String)su.getRequest().getParameter("ComID1");
		ReoverCommodity.setComName(ComID1);
		ReoverCommodity.setShaDate(ReoverCommodity.RCTime);
		String SellerPh1=(String)su.getRequest().getParameter("SellerPh1");
		ReoverCommodity.setSellerPh(SellerPh1);
		String SellerName1=(String)su.getRequest().getParameter("SellerName1");
		ReoverCommodity.setSellerName(SellerName1);
		String DesCribe1=(String)su.getRequest().getParameter("DesCribe1");
		ReoverCommodity.setDesCribe(DesCribe1);
		float k=(float)Double.parseDouble((String)su.getRequest().getParameter("ComPrice1"));
		ReoverCommodity.setComPrice(k);
		ReoverCommodity.setRecency(Integer.parseInt((String)su.getRequest().getParameter("Recency")));
		String ComInf1=(String)su.getRequest().getParameter("ComInf1");
		ReoverCommodity.setComInf(ComInf1);
		ReoverCommodity.setComImage(saveur);
	    if(ReoverCommodity.AddRoverCommodity(ReoverCommodity.getComID(),ReoverCommodity.getRovID(),ReoverCommodity.getComName(),ReoverCommodity.getShaDate(),ReoverCommodity.getSellerPh(),ReoverCommodity.getSellerName(),
		    ReoverCommodity.getDesCribe(),ReoverCommodity.getComImage(),ReoverCommodity.getComPrice(),ReoverCommodity.getComInf(),ReoverCommodity.getRecency())==0)
		{
	    
		   out.print("<script>alert('上传商品信息失败 ，请重新尝试  ')</script>");
	    }
	    else
	    {
		   out.print("<script>alert('"+ReoverCommodity.getShaDate()+"上传商品成功，商品编号为" +ReoverCommodity.getComID()+"')</script>");
		   
	    }
%>
   <script type="text/javascript">

   window.location.href="../index.jsp";    

  </script>
  </body>
</html>
